﻿#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct
{
    int age;
}content;


int main(){
    int shm_id;
    content *shm_content;
    key_t key;
    key = ftok("shm_test_key",0);
    shm_id = shmget(key,10,IPC_CREAT);
    if(shm_id == -1)
        perror("shmget error");
    shm_content = (content *)shmat(shm_id,NULL,0);
    int i = 0;
    for( i = 0; i<10; i++ ){
        printf("%d age:%d\n",getpid(),i);
        (*(shm_content+i)).age = i;
    }
    if(shmdt(shm_content) == -1)
        perror("detach error");
    return 0;
}
